Telegram Group & Telegram Channel
Sorts one list based on another list containing the desired indexes.

Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.

Use a list comprehension to get the first element of each pair from the result.

Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.

CODE:

def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]

Example:

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']

b = [3, 2, 6, 4, 1, 5]

sort_by_indexes(a, b)

Output:
['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']

sort_by_indexes(a, b, True)

Output:
['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']

Share and Support
@Python_Codes



tg-me.com/python_codes/179
Create:
Last Update:

Sorts one list based on another list containing the desired indexes.

Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.

Use a list comprehension to get the first element of each pair from the result.

Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.

CODE:

def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]

Example:

a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']

b = [3, 2, 6, 4, 1, 5]

sort_by_indexes(a, b)

Output:
['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']

sort_by_indexes(a, b, True)

Output:
['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']

Share and Support
@Python_Codes

BY Python Codes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_codes/179

View MORE
Open in Telegram


Python Codes Telegram | DID YOU KNOW?

Date: |

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

Python Codes from it


Telegram Python Codes
FROM USA